package com.zzh.service.impl.authentication;


import com.alibaba.fastjson.JSON;
import com.zzh.entity.Result;
import com.zzh.utils.ResultUtil;
import com.zzh.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wenrouyue
 * @FileName: AuthenticationEntryPointImpl
 * @description
 * @create 2023/2/10-17:59
 **/
@Service
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
        Result<String> error = ResultUtil.getError(HttpStatus.UNAUTHORIZED.value(), "用户认证失败!", "用户认证失败!");
        String rToString = JSON.toJSONString(error);
        WebUtils.renderString(response,rToString);
    }
}
